<?php
class Category_Form_QuickSearchJobForm extends Ac_Form
{
    private $_preview_link = '';

    private $_category;
    private $_adid;

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
    );

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
    );

    protected $formDecorators = array(
            'PrepareElements',
            array('ViewScript', array('viewScript' => 'index/customization/job.phtml'))
    );

    protected $subFormDecorators = array(
        'FormElements',
        array('Description', array('placement' => 'prepend')),
        'fieldset',
    );

    protected $spanDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $displayGroupDecorators = array(
        'FormElements',
        array('HtmlTag', array('tag' => 'p', 'class' => 'price radio text')),
        array('Description', array('placement' => 'prepend', 'tag' => 'label')),
    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
    );

    protected $elementRadioDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $elementSelectDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array()),
        array(array('row' => 'HtmlTag'), array('class'=>'select')),
    );

    public function __construct($options = array())
    {
        $this->_preview_link = !empty($options['preview_link']) ? $options['preview_link'] : '';
        $this->_category = !empty($options['category']) ? $options['category'] : array();
        $this->_adid = !empty($options['adid']) ? $options['adid'] : array();
        parent::__construct($options);
    }

    public function init()
    {
        parent::init();

        $this->setMethod('get');
        $this->setAction($this->getView()->geturl('regular' ,'search'));

        $element = new Zend_Form_Element_Hidden('adid');
        $element->setValue((string) $this->_adid);
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        //Category
        $this->addElement('hidden', 'small_category_list', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'value' => 'job',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
        
        // Employment Type
        $this->addElement('multiCheckbox', 'employmenttype', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('NotEmpty', true, array('messages' => 'This information is required')),
            ),
            'decorators'   => array(
                'ViewHelper',
                'Errors',
                array('Label', array()),
            ),
            'multiOptions' => Model_Listing_Job::getEmploymentTypePostAd(),
            'required'     => true,
        ));        

        
        // Job Location
        $this->addElement('multiCheckbox', 'joblocation', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('NotEmpty', true, array('messages' => 'This information is required')),
            ),
            'decorators'   => array(
                'ViewHelper',
                'Errors',
                array('Label', array()),
            ),
            'multiOptions' => Model_Listing_Job::getJobLocationPostAd(),
            'required'     => true,
        ));       

        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Search',
            'decorators'  => $this->elementButtonDecorators,
        ));

    }

    public function __toString()
    {
        $str = '
            $j("#jobcategory").change(function (event) {
                event.stopImmediatePropagation();
                $j.get("/search/get-attribute", { c:"car", f:"model", id: $j("#jobcategory").val() },
                    function(data) {
                        $j("#jobfunction").empty();
                        $j.each(data, function (index, value) {
                            $(document.createElement("option"))
                                .html(value)
                                .attr("value", index)
                                .appendTo($("#jobfunction"));
                        });
                    });
           });
        ';
        $this->view->JQuery()->addJavascript($str);
        return parent::__toString();
    }
}